package org.example.controller.order;

import org.example.dao.dto.order.OrderDto;
import org.example.dao.dto.order.OrderListDto;
import org.example.dao.dto.order.OrderWx;
import org.example.dao.entity.order.OOrder;
import org.example.dao.vo.OrderVo;
import org.example.exception.ParamLossErrorException;
import org.example.service.order.OrderService;
import org.example.skytool.commom.ParamUtil;
import org.springframework.web.bind.annotation.*;
import org.example.dao.commom.Result;
import org.example.dao.commom.ResultCode;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

/**
 * (Order)控制层
 *
 * @author makejava
 * @since 2023-05-10 10:55:46
 */
@RestController
@RequestMapping("/order")
@CrossOrigin
public class OrderController {

    @Resource
    private OrderService orderService;

    //TODO 功能

    /**
     * 预支付订单(用户)
     * 创建出订单，将创建的订单加入缓存（5min）
     * 返回订单确认页需要展示的信息
     */
    @PostMapping("prepayOrder")
    public Result prepayOrder(@RequestBody OrderVo orderVo) {
        List<OOrder> list = orderVo.getList();
        if(list==null||list.size()==0){
            throw new ParamLossErrorException("不能下空订单");
        }
        if(list.size()>20){
            throw new ParamLossErrorException("请不要一次性选择过多商品哦~");
        }
        HashMap<Long,Integer> map = new HashMap<>();
        for(OOrder order : list){
            ParamUtil.LongParam(order.getCommodityId());
            ParamUtil.IntegerParam(order.getCommodityNum());
            map.put(order.getCommodityId(),order.getCommodityNum());
        }
        OrderDto orderDto = orderService.prepareOrder(map);
        return new Result(ResultCode.SUCCESS,orderDto);
    }

    /**
     * 订单确认页确认创建
     * 从缓存种拿出该订单信息
     * 创建数据库订单，访问微信接口，调起支付
     * 将订单加入到 待付款 列表（15min）
     * 返回前端需要调起支付的数据信息
     */
    @PostMapping("createOrder")
    public Result createOrder(@RequestBody OrderVo orderVo){
        String tempTradeNo = orderVo.getTempTradeNo();
        Long addressId = orderVo.getAddressId();
        ParamUtil.StringParam(tempTradeNo);
        ParamUtil.LongParam(addressId);
        OrderWx result = orderService.createOrder(tempTradeNo,addressId);
        return new Result(ResultCode.SUCCESS,result);
    }


    /**
     * 前端判断用户完成支付
     * 从待支付列表查询 订单
     * 查找订单状态
     */
    @PostMapping("finishOrder")
    public Result finishOrder(@RequestBody OrderVo orderVo){
        String tempTradeNo = orderVo.getTempTradeNo();
        ParamUtil.StringParam(tempTradeNo);
        orderService.finishOrder(tempTradeNo);
        return new Result(ResultCode.SUCCESS,null);
    }


    /**
     * 获取用户订单根据订单状态(用户)
     */
    @PostMapping("/selectOrderByStatus/{start}/{limit}")
    public Result selectOrderByStatus(@PathVariable("start")Integer start,
                                      @PathVariable("limit")Integer limit,
                                      @RequestBody OOrder order) {
        String status = order.getStatus();

        ParamUtil.StringParam(status);

        List<OrderListDto> list = orderService.selectOrderByStatus(status,start,limit);

        return new Result(ResultCode.SUCCESS, list);
    }

    /**
     * 获取订单详情
     */
    @PostMapping("getOrderDetails")
    public Result getOrderDetails(@RequestBody OOrder order){
        String tradeNo = order.getTradeNo();

        ParamUtil.StringParam(tradeNo);

        OrderDto orderDto = orderService.getOrderDetails(tradeNo);

        return new Result(ResultCode.SUCCESS,orderDto);
    }




}

